package com.yunquan.ohana.ui.selectFrames.adapter

import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.yunquan.ohana.R
import com.yunquan.ohana.entity.FramesEntity

class SelectFramesAdapter(
    private val checkModel: Boolean,
    private val data: List<FramesEntity>
) : RecyclerView.Adapter<SelectFramesAdapter.ItemViewHolder>() {

    private var mCheckedListener: OnItemCheckedListener? = null
    private var mShareListener: OnItemShareListener? = null

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
        val view = View.inflate(parent.context, R.layout.item_frames2, null)
        return ItemViewHolder(view)
    }

    override fun getItemCount(): Int {
        return data.size
    }

    override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
        val item = data[position]
        holder.bind(item)
    }

    fun isGroupHeader(position: Int): Boolean {
        if (position == 0) {
            return true
        } else if (position >= data.size) {
            return false
        } else {
            val last = data[position - 1].online
            val current = data[position].online
            return last != current
        }
    }

    fun getItemData(position: Int): FramesEntity {
        return data[position]
    }

    fun setOnItemCheckedListener(listener: OnItemCheckedListener) {
        this.mCheckedListener = listener
    }

    fun setOnItemShareListener(listener: OnItemShareListener) {
        this.mShareListener = listener
    }

    inner class ItemViewHolder(val view: View) : RecyclerView.ViewHolder(view) {
        private val tvFramesName = view.findViewById<TextView>(R.id.tv_frames_name)
        private val btnShare = view.findViewById<TextView>(R.id.btn_share)
        private val btnCheck = view.findViewById<ImageView>(R.id.btn_check)

        fun bind(item: FramesEntity) {
            tvFramesName.text = item.name
            if (item.check) {
                btnCheck.setImageResource(R.mipmap.ic_check2_true)
            } else {
                btnCheck.setImageResource(R.mipmap.ic_check2_false)
            }

            if (checkModel) {
                btnCheck.visibility = View.VISIBLE
                btnShare.visibility = View.GONE
            } else {
                btnCheck.visibility = View.GONE
                btnShare.visibility = View.VISIBLE
            }

            btnCheck.setOnClickListener {
                item.check = !item.check
                mCheckedListener?.onItemChecked(item.check, item)
                if (item.check) {
                    btnCheck.setImageResource(R.mipmap.ic_check2_true)
                } else {
                    btnCheck.setImageResource(R.mipmap.ic_check2_false)
                }
            }

            btnShare.setOnClickListener {
                mShareListener?.onItemShare(item)
            }
        }

    }

    interface OnItemCheckedListener {
        fun onItemChecked(check: Boolean, item: FramesEntity)
    }

    interface OnItemShareListener {
        fun onItemShare(item: FramesEntity)
    }
}